package cc.messcat.service.helppeson;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cc.messcat.common.model.Pager;
import cc.messcat.common.service.BaseServiceImpl;
import cc.messcat.common.util.BeanUtil;
import cc.messcat.dao.helpTeam.HelpTeamDao;
import cc.messcat.dao.helpperson.HelppersonDao;
import cc.messcat.dao.helpunits.HelpunitsDao;
import cc.messcat.entity.fupin.PoorFamily;
import cc.messcat.entity.helpPerson.HelpPerson;
import cc.messcat.entity.helpUnits.HelpTeamPerson;
import cc.messcat.entity.helpUnits.HelpUnits;
import cc.messcat.entity.system.McUser;

@Service
public class HelppesonServiceImpl extends BaseServiceImpl<HelpPerson> implements HelppesonService {

    @Autowired
    private HelppersonDao helppersonDao;
    
    @Autowired
    private HelpunitsDao helpUnitDao;

    @Resource
    private HelpTeamDao helpTeamDao;

    @Override
    public void addHelppeson(HelpPerson helpPerson, Long HELP_UNIT) {
        HelpUnits helpUnits = retrieveObject(HelpUnits.class, HELP_UNIT);
        helpPerson.setUnit_name(helpUnits.getUnit_name());
        helpPerson.setDate(new Date());
        helpPerson.setUpdate_time(new Date());
        helpPerson.setHelpUnit(HELP_UNIT);
        helpPerson.setStatus("1");
        addObject(helpPerson);
    }

    @Override
    public void modifyHelppeson(HelpPerson helpPerson) {
        helpPerson.setUpdate_time(new Date());
        modifyObject(helpPerson);
    }

    @Override
    public Pager<HelpPerson> getPage(int pageNo, int pageSize, String Affiliation, String Unit_name, String telephone_number) {
        Map map = new HashMap<>();
        StringBuffer hql = new StringBuffer();
        hql.append("FROM HelpPerson WHERE 1=1 ");
        if (Affiliation != null && !"".equals(Affiliation)) {
            Affiliation = Affiliation.replaceAll(" ", "");
            hql.append(" AND Affiliation like '%" + Affiliation + "%'");
        }
        if (Unit_name != null && !"".equals(Unit_name)) {
            Unit_name = Unit_name.replaceAll(" ", "");
            hql.append(" AND Unit_name like '%" + Unit_name + "%'");
        }
        if (telephone_number != null && !"".equals(telephone_number)) {
            telephone_number = telephone_number.replaceAll(" ", "");
            hql.append(" AND telephone_number like '%" + telephone_number + "%'");
        }
        hql.append(" AND status = '1' ");
        return this.helppersonDao.getPagerByHql(pageNo, pageSize, hql.toString(), map);
    }

    @Override
    public Pager<HelpPerson> getHelpPersonInHelpUnitsByArea(int pageNo, int pageSize, String Affiliation, String Unit_name, String telephone_number, List<Long> helpUnitIds) {
        Map map = new HashMap<>();
        StringBuffer hql = new StringBuffer();
        hql.append("FROM HelpPerson WHERE 1=1 ");
        if (Affiliation != null && !"".equals(Affiliation)) {
            Affiliation = Affiliation.replaceAll(" ", "");
            hql.append(" AND Affiliation like '%" + Affiliation + "%'");
        }
        if (Unit_name != null && !"".equals(Unit_name)) {
            Unit_name = Unit_name.replaceAll(" ", "");
            hql.append(" AND Unit_name like '%" + Unit_name + "%'");
        }
        if (telephone_number != null && !"".equals(telephone_number)) {
            telephone_number = telephone_number.replaceAll(" ", "");
            hql.append(" AND telephone_number like '%" + telephone_number + "%'");
        }
        if (BeanUtil.isValid(helpUnitIds)) {
            String str = "0";
            for (Long helpUnitId : helpUnitIds)
                str += " , " + helpUnitId;
            hql.append(" AND helpUnit in (" + str + ")");
        }
        hql.append(" AND status = '1' ");
        return this.helppersonDao.getPagerByHql(pageNo, pageSize, hql.toString(), map);
    }

    @Override
    public List<HelpPerson> helpPerson(Long id) {
        Map map = new HashMap<>();
        String hql = "from HelpPerson where helpUnit= :HELP_UNIT  AND status = '1' ";
        map.put("HELP_UNIT", id);
        return this.helppersonDao.queryList(hql, map);
    }

    @Override
    public HelpPerson helpPersons(Long id) {

        return this.helppersonDao.get(id);
    }


    public void modifyHelppesons(HelpPerson helpPerson, Long HELP_UNIT) {
        helpPerson.setUpdate_time(new Date());
        helpPerson.setHelpUnit(HELP_UNIT);
        modifyObject(helpPerson);
    }

    @Override
    public List<HelpUnits> helpunits(Long id) {
        Map map = new HashMap<>();
        String hql = "from HelpUnits where id= :id";
        map.put("id", id);
        return this.helppersonDao.queryList(hql, map);
    }

    @Override
    public Pager getHelpPresonsNotInTeam(Integer pageNo, Integer pageSize, Long unitId, Long villageId) {
        List<HelpPerson> result = helpTeamDao.getHelpPresonsNotInTeam(unitId, villageId);
        return super.getPager(pageNo, pageSize, result, result.size());
    }

    @SuppressWarnings("unchecked")
	@Override
    public void modifyHelppesonunit(HelpPerson helpPerson, Long HELP_UNIT,String unit_name) {
        helpPerson.setHelpUnit(HELP_UNIT);
        helpPerson.setUnit_name(unit_name);
        
        List<PoorFamily> poorFamilys = helppersonDao.listPoorFamily(helpPerson.getId());
        HelpUnits helpUnit = helpUnitDao.get(HELP_UNIT);
        
//        List<PoorVillage> poorVillages = helpUnit.getPoorVillages();
//        if(!Collections.isEmpty(poorFamilys)) {
//        	ArrayList<PoorFamily> unAffectedPF = new ArrayList<>();
//        	for(PoorVillage e : poorVillages) {
//        		unAffectedPF = Collections.select(poorFamilys, e.getArea().getId(), new Comparator<Object>() {
//        			@Override
//        			public int compare(Object o1, Object o2) {
//        				return ((PoorFamily)o1).getArea().getId().equals((Long)o2) ? 0 : 1; 
//        			}
//        		});
//        	}
//        	helppersonDao.updateHelpPeronAndPoorFamily(unAffectedPF,helpPerson.getId());
//        }
        
        modifyObject(helpPerson);
    }

    @Override
    public Pager getPageNew(Integer pageNo, Integer pageSize,
                            String Affiliation, String Unit_name, String telephone_number) {

        String sql = "select hp.*,htp.unit_name as helpUnitName from HelpPerson hp LEFT JOIN helpunits htp on hp.help_unit=htp.id where 1=1 ";
        StringBuffer condition = new StringBuffer();
        condition.append(sql);
//        hql.append("FROM HelpPerson WHERE 1=1 ");

        if (Affiliation != null && !"".equals(Affiliation)) {
            Affiliation = Affiliation.replaceAll(" ", "");
            condition.append(" AND hp.Affiliation like '%" + Affiliation + "%'");
        }
        if (Unit_name != null && !"".equals(Unit_name)) {
            Unit_name = Unit_name.replaceAll(" ", "");
            condition.append(" AND hp.Unit_name like '%" + Unit_name + "%'");
        }
        if (telephone_number != null && !"".equals(telephone_number)) {
            telephone_number = telephone_number.replaceAll(" ", "");
            condition.append(" AND hp.telephone_number like '%" + telephone_number + "%'");
        }
        condition.append(" AND hp.status = '1' ");


        return getPager(pageNo, pageSize, this.helppersonDao.getPageNew(sql), this.helppersonDao.getPageNew(sql).size());
//        return this.helppersonDao.getPagerByHql(pageNo, pageSize, condition.toString(), map);
    }

    /**
     * 获取新实体类的通用方法
     * @param helpPerson
     * @return
     */
    @Override
    public HelpPerson getHelpPerson(HelpPerson helpPerson){
    	HelpPerson helpPerson0 = new HelpPerson();
    	helpPerson0.setId(helpPerson.getId());
    	helpPerson0.setDuty_id(helpPerson.getDuty_id());
    	helpPerson0.setDate(helpPerson.getDate());
    	helpPerson0.setName(helpPerson.getName());
    	helpPerson0.setSex(helpPerson.getSex());
    	helpPerson0.setID_number(helpPerson.getID_number());
    	helpPerson0.setJob_level(helpPerson.getJob_level());
    	helpPerson0.setFace(helpPerson.getFace());
    	helpPerson0.setEducation(helpPerson.getEducation());
    	helpPerson0.setTechnical(helpPerson.getTechnical());
    	helpPerson0.setTelephone_number(helpPerson.getTelephone_number());
    	helpPerson0.setPrepared_by(helpPerson.getPrepared_by());
    	helpPerson0.setPrepared_time(helpPerson.getPrepared_time());
    	helpPerson0.setModifier(helpPerson.getModifier());
    	helpPerson0.setUpdate_time(helpPerson.getUpdate_time());
    	helpPerson0.setHelpUnit(helpPerson.getHelpUnit());
    	helpPerson0.setAffiliation(helpPerson.getAffiliation());
    	helpPerson0.setUnit_name(helpPerson.getUnit_name());
    	helpPerson0.setStatus(helpPerson.getStatus());
    	helpPerson0.setHelpUnitName(helpPerson.getHelpUnitName());
    	return helpPerson0;
    }

	@Override
	public List<HelpTeamPerson> getPerson_id(Long a) {
		Map map = new HashMap<>();
        String hql = "from HelpTeamPerson where helpPerson.id= :helpPerson";
        map.put("helpPerson", a);
        return  this.helppersonDao.queryList(hql, map);
	}

	@Override
	public void modifyHelppeson(HelpTeamPerson helpTeamPerson) {
		modifyObject(helpTeamPerson);
	}

	@Override
	public boolean helppesonJudge(Long id) {
		return helppersonDao.helppesonJudge(id);
	}

	@Override
	public void updateHelpPersonAndPoorFamily(Long id) {
		helppersonDao.updateHelpPersonAndPoorFamily(id);
	}

	@Override
	public List<Map<Object, Object>> checkHelpPersonAndPoorFamily(Long id) {
		return helppersonDao.checkHelpPersonAndPoorFamily(id);
	}

	@Override
	public List<HelpPerson> listHelpPerson(String helpPersonIds) {
		return helppersonDao.queryList(" from HelpPerson where status='1' and id in("+helpPersonIds+")",null);
	}

	/**
	 * 按条件列出帮扶责任人
	 * @return
	 */
	@Override
	public Pager listHelpPerson(Integer pageNo, Integer pageSize, McUser user,String unitName, String name, String idNumber,
		String tel) {
		
		return helppersonDao.listHelpPerson(pageNo,pageSize,user,unitName,name,idNumber,tel);
	}

	@Override
	public List<Map<String, Object>> listPoorFamilyByHelpPerson(Long helpPersonID) {
		return helppersonDao.listPoorFamilyByHelpPerson(helpPersonID);
	}

	/**
	 * 通过帮扶责任人ID查询帮扶单位
	 */
	@Override
	public List<HelpUnits> getHelpUnitByHelpPersonID(Long id) {
		return helppersonDao.getHelpUnitByHelpPersonID(id);
	}
}
